package com.handinfo.uimenu.window;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameMIDlet;
import com.handinfo.engine.GameManager;
import com.handinfo.engine.net.GameProtocol;
import com.handinfo.game.ActorItem;
import com.handinfo.game.ActorItemEquip;
import com.handinfo.game.ActorRole;
import com.handinfo.game.MailItem;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIListener;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIMessageBox;
import com.handinfo.uimenu.UIPopmenu;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.ArrayList;
import com.handinfo.util.Tools;
import com.test.touch.GameTouchManager;

public class UIMail extends UIAbstractWindow implements ILBHandler
{
	// 状态
	private static final int STATE_BASE_WRITE = 1;// 写邮件
	private static final int STATE_BASE_RECV = 2;// 收邮件
	private static final int STATE_BASE_ADD = 3;// 添加邮件
	private static final int STATE_BASE_WRITE_FAMILY = 4;// 发邮件给家族
	private static final int STATE_BASE_WRITE_GM = 5;// 发邮件给GM
	// NUM
	private static final int NUM_COLS_ITEMS = 1;// 邮件中的道具最大个数
	// Index - recv && write
	private final int GROUP_MAIL_TITLE = 1;
	private final int GROUP_MAIL_RECVER = 2;
	private final int GROUP_MAIL_LIST_SOCIAL = 3;
	private final int GROUP_MAIL_ITEMS = 4;
	private final int GROUP_MAIL_MONEY_SEND = 5;
	private final int GROUP_MAIL_CONTENT = 6;
	//
	private final int GROUP_ADD_ITEMS = 8;
	private final int GROUP_ADD_LABEL = 9;
	private final int GROUP_ADD_BAG = 10;
	//
	private static final int INDEX_MAIL_TITLE = 1;
	private static final int INDEX_MAIL_RECVER = 2;
	private static final int INDEX_MAIL_LIST_SOCIAL = 3;
	private static final int INDEX_MAIL_ITEMS_FIRST = 4;
	private static final int INDEX_MAIL_ITEMS_LAST = INDEX_MAIL_ITEMS_FIRST + NUM_COLS_ITEMS - 1;
	private static final int INDEX_MAIL_MONEY_SEND = INDEX_MAIL_ITEMS_LAST + 1;
	private static final int INDEX_MAIL_CONTENT = INDEX_MAIL_MONEY_SEND + 1;
	// Index -mail add
	private static final int INDEX_ADD_ITEMS_FIRST = 1;
	private static final int INDEX_ADD_ITEMS_LAST = INDEX_ADD_ITEMS_FIRST + NUM_COLS_ITEMS - 1;
	private static final int INDEX_ADD_LABEL_FIRST = INDEX_ADD_ITEMS_LAST + 1;
	private static final int INDEX_ADD_LABEL_LAST = INDEX_ADD_LABEL_FIRST + ActorItemLBHandler.NUM_LABEL_DIS - 1;
	private static final int INDEX_ADD_BAG_FIRST = INDEX_ADD_LABEL_LAST + 1;
	//
	private final String TITLE_ADD_SOCIAL = "添加";

	// Var - mail
	private byte m_mailType;
	private long m_mail_id;
	private String m_mail_title = "";
	private String m_mail_name_recver = "";
	private String m_mail_content = "";// 信件内容
	// private String m_mail_time = "";
	private String[] m_mail_contents;
	// private long m_mail_id_recer = -1;
	private String m_mail_name_sender = "";
	private long m_mail_money_game_send;// 放入到邮件中的游戏币
	private ActorItem[] m_mail_items = new ActorItem[NUM_COLS_ITEMS];
	private ActorItem[] m_mail_items_temp;
	// Var - add
	// private long m_mail_money_game_now = GameManager.getInstance().m_role.m_money_game; // 当前游戏币
	private ActorItem[] m_items_bag = ActorRole.getActorItemCopys(GameManager.getInstance().m_role.m_bag_goods);
	private ActorItem[] m_items_bag_temp;
	private ActorItemLBHandler m_bag_handler;
	private int m_index_item;
	private String m_title;

	private final String TITLE_ATT = "标题";
	private final String RECVER_ATT = "收件人";
	private final String SENDER_ATT = "发件人";
	private final String ACCESSARY_ATT = "附件";
	private final String MONEY_SEND_ATT_ADD = "添加金钱";
	private final String MONEY_SEND_ATT_RECV = "金钱";
	private final int H_PRE_TITLE = UITools.H_LABEL_STR;
	private final int H_TITLEANDRECVER = ((UITools.H_LABEL_STR << 1) + (UITools.IW_RECT_CONTENT << 1)) + 1;// 中间一像素
	private final int H_MONEY = ((UITools.H_PRE + UITools.IH_RECT_CONTENT) << 1);
	private final int W_TITLE = UITools.W_RECT - UITools.IW_RECT_CONTENT
			- AbstractGameScreen.s_font.stringWidth(SENDER_ATT) - (UIManager.getColorsScrollBar().length << 1);
	private final int W_ADD = (AbstractGameScreen.FONT_W*2) + (UITools.IW_WIN<<1);
	private final int W_RECVER = W_TITLE - W_ADD - UITools.IW_RECT_CONTENT;
	private int m_h_contents = UITools.H_RECT - (H_TITLEANDRECVER + UITools.IH_RECTS) - (H_MONEY + UITools.IH_RECTS);

	// var window

	public UIMail(int id)
	{
		super(id);
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);
	}

	public int countGroupId(int mIndex)
	{
		switch (this.m_state) {
			case STATE_BASE_WRITE:
			case STATE_BASE_WRITE_FAMILY:
			case STATE_BASE_WRITE_GM:
				if (mIndex < INDEX_MAIL_RECVER) {
					return GROUP_MAIL_TITLE;
				}
				else if (mIndex < INDEX_MAIL_LIST_SOCIAL) {
					return GROUP_MAIL_RECVER;
				}
				else if (mIndex < INDEX_MAIL_ITEMS_FIRST) {
					return GROUP_MAIL_LIST_SOCIAL;
				}
				else if (mIndex < INDEX_MAIL_MONEY_SEND) {
					return GROUP_MAIL_ITEMS;
				}
				else if (mIndex < INDEX_MAIL_CONTENT) {
					return GROUP_MAIL_MONEY_SEND;
				}
				else {
					return GROUP_MAIL_CONTENT;
				}
			case STATE_BASE_RECV:
				return GROUP_MAIL_ITEMS;
			case STATE_BASE_ADD:
				if (mIndex < INDEX_ADD_LABEL_FIRST) {
					return GROUP_ADD_ITEMS;
				}
				else if (mIndex < INDEX_ADD_BAG_FIRST) {
					return GROUP_ADD_LABEL;
				}
				else {
					return GROUP_ADD_BAG;
				}
			default:
				return GROUP_ADD_BAG;//
		}
	}

	public int countGroupIndex(int mGroupId, int mIndex)
	{
		switch (this.m_state) {
			case STATE_BASE_WRITE:
			case STATE_BASE_WRITE_FAMILY:
			case STATE_BASE_WRITE_GM:
				switch (mGroupId) {
					case GROUP_MAIL_TITLE:
					case GROUP_MAIL_MONEY_SEND:
					case GROUP_MAIL_LIST_SOCIAL:
					case GROUP_MAIL_RECVER:
					case GROUP_MAIL_CONTENT:
						return 0;
					case GROUP_MAIL_ITEMS:
						return mIndex - INDEX_MAIL_ITEMS_FIRST;
				}
				break;
			case STATE_BASE_RECV:
				return 0;
			case STATE_BASE_ADD:
				switch (mGroupId) {
					case GROUP_ADD_ITEMS:
						return mIndex - INDEX_ADD_ITEMS_FIRST;
					case GROUP_ADD_LABEL:
						return mIndex - INDEX_ADD_LABEL_FIRST;
					case GROUP_ADD_BAG:
						return mIndex - INDEX_ADD_BAG_FIRST;
				}
				break;
		}
		return 1;
	}

	public void actionOnIndexChanged()
	{
		switch (m_state) {
			case STATE_BASE_ADD:
				switch (m_groupId) {
					case GROUP_ADD_LABEL:
						// m_bag_handler.setLabelByGroupIndex(this.m_groupIndex);
						break;
					case GROUP_ADD_ITEMS:
						m_index_item = this.m_groupIndex;
						break;
				}
				break;
			case STATE_BASE_WRITE:
				switch (m_groupId) {
					case GROUP_MAIL_ITEMS:
						m_index_item = this.m_groupIndex;
						break;
				}
		}
		if (m_bag_handler != null) {
			m_bag_handler.updateOnStateOrIndexChanged();
		}
	}

	public void actionOnStateChanged()
	{
		
		// 初始化
		switch (m_state) {
			case STATE_BASE_RECV:
				m_bag_handler = null;
				m_mail_items_temp = null;
				m_items_bag_temp = null;
				m_title = "收件箱";
				break;
			case STATE_BASE_WRITE:
				m_bag_handler = null;
				m_mail_items_temp = null;
				m_items_bag_temp = null;
				m_title = "写邮件";
				break;
			case STATE_BASE_WRITE_FAMILY:
				m_bag_handler = null;
				m_mail_items_temp = null;
				m_items_bag_temp = null;
				m_title = "群邮件";
				break;
			case STATE_BASE_WRITE_GM:
				m_bag_handler = null;
				m_mail_items_temp = null;
				m_items_bag_temp = null;
				m_title = "联系客服";
				break;
			case STATE_BASE_ADD:
				m_mail_items_temp = ActorRole.getActorItemCopys(m_mail_items);
				m_items_bag_temp = ActorRole.getActorItemCopys(m_items_bag);
				m_bag_handler = new ActorItemLBHandler(this);
				m_bag_handler.setLabelByLabelId(ActorItem.TYPE_GOODS_ALL);
				setWindowIndex(INDEX_ADD_BAG_FIRST);
				m_title = "写邮件";
				break;
			default:
				break;
		}
		registerTouch();
	}

	/***************************/
	// 开启Menu
	/***************************/

	/**
	 * 传入一个道具，并打开发送邮件界面
	 * 
	 * @param item
	 */
	public static final void openSendMail(ActorItem item)
	{
		UIMail mail = (UIMail) UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_MAIL);
		mail.m_mail_items = new ActorItem[NUM_COLS_ITEMS];
		mail.m_mail_items[0] = item;
		mail.setWindowStatus(STATE_BASE_WRITE);
		mail.setWindowIndex(INDEX_MAIL_TITLE);

	}

	/**
	 * 发送给家族 2012-2-15 下午03:29:25 venizeng
	 */
	public static final void sendMailToFamily()
	{
		gotoMail("全体家族成员", STATE_BASE_WRITE_FAMILY, INDEX_MAIL_TITLE);
	}

	/**
	 * 发送给家族 2012-2-15 下午03:29:25 venizeng
	 */
	public static final void sendMailToGM()
	{
		gotoMail("GM", STATE_BASE_WRITE_GM, INDEX_MAIL_TITLE);
	}

	/**
	 * 发消息给玩家 2012-2-16 上午11:18:04 venizeng
	 */
	public static final void sendMailToPlayer(String recverName)
	{
		gotoMail(recverName, STATE_BASE_WRITE, INDEX_MAIL_TITLE);
	}

	/**
	 * 外部进入邮件系统
	 * 2012-4-6 下午03:58:52
	 * venizeng
	 * @param recvName
	 * @param state
	 * @param index
	 */
	private static void gotoMail(String recverName, int state, int index)
	{
//		boolean isExist = true;
//		UIMail mail = (UIMail) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_MAIL);
//		if (mail == null) {
//			isExist = false;
//			mail = (UIMail) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_MAIL);
//		}
		UIMail mail = (UIMail) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_MAIL);
		mail.m_mail_title = "";
		mail.m_mail_content = "";
		mail.changeContent(mail.m_mail_content);
		mail.m_mail_name_sender = GameManager.getInstance().m_role.m_name;
		mail.m_mail_name_recver = recverName;
		mail.setWindowStatus(state);
		mail.setWindowIndex(index);
//		if (!isExist) {
			UIManager.getInstance().addWindow(mail);
//		}
	}

	/**
	 * 获得社交列表反馈的关系人信息
	 * @param data
	 */
	public static final void recvSocialInfo(Object data)
	{
		UIMail mail = (UIMail) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_MAIL);
		if (mail != null) {
			// mail.m_mail_name_recver;
			// mail.m_mail_id_recver;
		}
	}

	// ////////////////////////
	// IBagHandler
	// ////////////////////////
	public Object[] getBagDatas()
	{
		return m_items_bag_temp;
	}

	public int getUpIndex()
	{
		switch (this.m_groupId) {
			case GROUP_ADD_LABEL:
				return INDEX_ADD_ITEMS_FIRST + m_index_item;
			case GROUP_ADD_BAG:
				return m_bag_handler.getCurrentLabelWindowIndex();
			default:
				Tools.debugPrintln("无此组Id = " + this.m_groupId);
				return 1;
		}
	}

	public int getDownIndex()
	{
		switch (this.m_groupId) {
			case GROUP_ADD_LABEL:
				return INDEX_ADD_BAG_FIRST;
			case GROUP_ADD_BAG:
				return m_bag_handler.getCurrentLabelWindowIndex();
			default:
				Tools.debugPrintln("无此组Id = " + this.m_groupId);
				return 1;
		}
	}

	public int getLeftIndex()
	{
		switch (this.m_groupId) {
			case GROUP_ADD_LABEL:
				return INDEX_ADD_LABEL_LAST;
			case GROUP_ADD_BAG:
				return INDEX_ADD_LABEL_LAST;
			default:
				Tools.debugPrintln("无此组Id = " + this.m_groupId);
				return 1;
		}
	}

	public int getRightIndex()
	{
		switch (this.m_groupId) {
			case GROUP_ADD_LABEL:
				return INDEX_ADD_LABEL_FIRST;
			case GROUP_ADD_BAG:
				return m_bag_handler.getCurrentLabelWindowIndex();
			default:
				Tools.debugPrintln("无此组Id = " + this.m_groupId);
				return 1;
		}
	}

	public int getFirstIndex(int idComp)
	{
		switch (idComp) {
			case AbsLBHandler.TYPE_BAG:
				return INDEX_ADD_BAG_FIRST;
			case AbsLBHandler.TYPE_LABEL:
				return INDEX_ADD_LABEL_FIRST;
			default:
				Tools.debugPrintln("无此组Id = " + this.m_groupId);
				return 1;
		}
	}

	public int getGroupIndex()
	{
		return this.m_groupIndex;
	}

	public int getWindowIndex()
	{
		return this.m_index;
	}

	/***************************/
	// 按键处理
	/***************************/

	private void changeContent(String content)
	{
		m_mail_content = content;
		if (m_mail_content == null || m_mail_content.equals("")) {
			m_mail_contents = null;
		}
		else {
			m_mail_contents = Tools.getStrArray(m_mail_content, AbstractGameScreen.s_font, UITools.W_RECT
					- (UITools.IW_RECT_CONTENT << 1));
		}
	}

	protected void keyLogic()
	{
		switch (this.m_state) {
			case STATE_BASE_RECV:
				keyLogicOnStateRecv();
				break;
			case STATE_BASE_WRITE:
				keyLogicOnStateWrite();
				break;
			case STATE_BASE_WRITE_FAMILY:
			case STATE_BASE_WRITE_GM:
				keyLogicOnSend();
				break;
			case STATE_BASE_ADD:
				keyLogicOnStateAdd();
				break;
		}
	}

	private boolean keyLogicOnWrite()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			close();
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
			if (m_mail_title.equals("")) {
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "请输入标题");
			}
			else if (m_mail_name_recver.equals("")) {
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "请输入收件人名称");
			}
			else {
				ArrayList list = new ArrayList();
				for (int i = 0; i < m_mail_items.length; i++) {
					if (m_mail_items[i] == null) {
						continue;
					}
					list.addElement(m_mail_items[i]);
				}
				int size = list.size();
				int[] actorItemIndexs = null;
				if (size != 0) {
					actorItemIndexs = new int[size];
					for (int i = 0; i < size; i++) {
						actorItemIndexs[i] = ((ActorItem) list.elementAt(i)).m_index;
					}
				}

				switch (m_state) {
					case STATE_BASE_WRITE_FAMILY:
						GameManager.getInstance().m_game_message.sendMailFamily(m_mail_title, m_mail_content);
						break;
					case STATE_BASE_WRITE_GM:
					default:
						GameManager.getInstance().m_game_message.sendMail(m_mail_name_recver, m_mail_title,
								m_mail_content, m_mail_money_game_send, actorItemIndexs);
						break;
				}
			}
		}
		else {
			return false;
		}
		return true;
	}

	private void keyLogicOnSend()
	{
		if (keyLogicOnWrite()) {
			return;
		}
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
			gotoWriteContentFrom();
		}
	}

	private void keyLogicOnStateWrite()
	{
		if (keyLogicOnWrite()) {
			return;
		}
		Tools.debugPrintln("this.m_groupID="+this.m_groupId);
		switch (this.m_groupId) {
			case GROUP_MAIL_TITLE:
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
					this.setWindowIndex(INDEX_MAIL_MONEY_SEND);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
					this.setWindowIndex(INDEX_MAIL_RECVER);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
					this.setWindowIndex(INDEX_MAIL_LIST_SOCIAL);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
					this.setWindowIndex(INDEX_MAIL_LIST_SOCIAL);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					gotoWriteContentFrom();
				}
				break;
			case GROUP_MAIL_RECVER:
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
					this.setWindowIndex(INDEX_MAIL_TITLE);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
					this.setWindowIndex(INDEX_MAIL_ITEMS_FIRST);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
					this.setWindowIndex(INDEX_MAIL_LIST_SOCIAL);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
					this.setWindowIndex(INDEX_MAIL_LIST_SOCIAL);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					switch (m_state) {
						case STATE_BASE_WRITE_FAMILY:
							GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B,
									"只能发邮件给全体成员，收件人不能修改");
							break;
						case STATE_BASE_WRITE_GM:
							GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B,
									"只能发邮件给GM，收件人不能修改");
							break;
						default:
							gotoWriteContentFrom();
							break;
					}
				}
			case GROUP_MAIL_CONTENT:
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					gotoWriteContentFrom();
				}
				break;
			case GROUP_MAIL_LIST_SOCIAL:
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
					this.setWindowIndex(INDEX_MAIL_MONEY_SEND);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
					this.setWindowIndex(INDEX_MAIL_MONEY_SEND);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
					this.setWindowIndex(INDEX_MAIL_RECVER);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
					this.setWindowIndex(INDEX_MAIL_RECVER);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					switch (m_state) {
						case STATE_BASE_WRITE_FAMILY:
							GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B,
									"只能发邮件给全体成员，不能去社交列表中查找收件人");
							return;
						case STATE_BASE_WRITE_GM:
							GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B,
									"只能发邮件给GM，不能去社交列表中查找收件人");
							return;
						default:
//							gotoWriteContentFrom();
							break;
					}
					final UIPopmenu popmenu = (UIPopmenu) UIManager.getInstance().createWindow(
								UIWindowFactory.WINDOW_POPMENU);
//					popmenu.setPopmenus(new String[] { "夫妻", "结义", "师徒", "家族", "好友", "仇敌" ,"黑名单"});
					popmenu.setPopmenus(new String[] { "好友", "仇敌","返回" });
					popmenu.addListener(new UIListener() {

						public void actionPerformed()
							{
								if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
										|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
									switch (popmenu.getCurrentIndex()) {// 0:好友 1：仇敌 5：黑名单
										case 0:// 好友
											UISocialWindow.MailToSocial(0);
											break;
										case 1:// 仇敌
											UISocialWindow.MailToSocial(1);
											break;
//										case 3://黑名单
//										default:
//											UISocialWindow.MailToSocial(5);
//											break;
									}
									UIManager.getInstance().removeWindow(popmenu);
								}
								else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
									UIManager.getInstance().removeWindow(popmenu);
								}
							}
					});
				}
				break;
			case GROUP_MAIL_ITEMS:
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
					this.setWindowIndex(INDEX_MAIL_RECVER);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
					this.setWindowIndex(INDEX_MAIL_MONEY_SEND);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
					if (getGroupIndex() == 0) {
						this.setWindowIndex(INDEX_MAIL_ITEMS_LAST);
					}
					else {
						this.setWindowIndex(this.m_index - 1);
					}
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
					if (getGroupIndex() == NUM_COLS_ITEMS - 1) {
						this.setWindowIndex(INDEX_MAIL_ITEMS_FIRST);
					}
					else {
						this.setWindowIndex(this.m_index + 1);
					}
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
							|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					if (this.m_state == STATE_BASE_WRITE_FAMILY) {// 家族邮件名不允许修改
						GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "不支持给全家族发道具");
						return;
					}
					this.setWindowStatus(STATE_BASE_ADD);
				}
				break;
			case GROUP_MAIL_MONEY_SEND:
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
					this.setWindowIndex(INDEX_MAIL_ITEMS_FIRST);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
					this.setWindowIndex(INDEX_MAIL_RECVER);
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					if (this.m_state == STATE_BASE_WRITE_FAMILY) {// 家族邮件名不允许修改
						GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "不支持给全家族发金钱");
						return;
					}
					gotoWriteMoneyForm();
				}
				break;
		}

	}
	

	private void keyLogicOnStateAdd()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {// 确定
			for (int i = 0; i < m_mail_items.length; i++) {
				m_mail_items[i] = m_mail_items_temp[i];
			}
			for (int i = 0; i < m_items_bag_temp.length; i++) {
				m_items_bag[i] = m_items_bag_temp[i];
			}
			this.setWindowStatus(STATE_BASE_WRITE);
			this.setWindowIndex(INDEX_MAIL_ITEMS_FIRST);
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {// 返回
			this.setWindowStatus(STATE_BASE_WRITE);
			this.setWindowIndex(INDEX_MAIL_ITEMS_FIRST);
		}
		else {
			switch (this.m_groupId) {
				case GROUP_ADD_ITEMS:
					if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
						// 卸下
						m_mail_items_temp[m_index_item] = null;
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
						this.setWindowIndex(INDEX_ADD_LABEL_FIRST);
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
						this.setWindowIndex(m_bag_handler.getCurrentLabelWindowIndex());
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
						if (this.m_index == INDEX_ADD_ITEMS_FIRST) {
							this.setWindowIndex(INDEX_ADD_ITEMS_LAST);
						}
						else {
							this.setWindowIndex(this.m_index - 1);
						}
					}
					else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
						if (this.m_index == INDEX_ADD_ITEMS_LAST) {
							this.setWindowIndex(INDEX_ADD_ITEMS_FIRST);
						}
						else {
							this.setWindowIndex(this.m_index + 1);
						}
					}
					break;
				case GROUP_ADD_LABEL: {
					int dir = m_bag_handler.keyDir(AbsLBHandler.TYPE_LABEL);
					if (dir == AbsLBHandler.TYPE_DIR_UNPRO) {

					}
					if (dir == AbsLBHandler.TYPE_DIR_NONE) {

					}
					else {
						this.setWindowIndex(dir);
					}
				}
					break;
				case GROUP_ADD_BAG: {
					int dir = m_bag_handler.keyDir(AbsLBHandler.TYPE_BAG);
					if (dir == AbsLBHandler.TYPE_DIR_UNPRO) {

					}
					if (dir == AbsLBHandler.TYPE_DIR_NONE) {
						if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
							ActorItem item = (ActorItem) m_bag_handler.getCurrentBagItem();
							if (item == null) {
								return;
							}
							if (item.m_isOperLocked) {
								GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "绑定的道具不能赠送给他人") ;
								return ;
							}
							int dataIndex = item.m_index;
							item = m_items_bag_temp[dataIndex];
							if (item == null) {
								return;
							}
							if (m_mail_items_temp[m_index_item] == null) {
								m_mail_items_temp[m_index_item] = item;
								m_items_bag_temp[dataIndex] = null;
							}
							else {
								ActorItem temp = m_mail_items_temp[m_index_item];
								m_mail_items_temp[m_index_item] = item;
								m_items_bag_temp[temp.m_index] = temp;
								m_items_bag_temp[dataIndex] = null;
								m_bag_handler.resetBagData();
							}
						}
					}
					else {
						this.setWindowIndex(dir);
					}
				}
					break;
			}
		}
	}

	private final String STR_POP_RECVALL = "附件全收";
	private final String STR_POP_MOVETOCOL = "移往收藏夹";
	private final String STR_POP_DELETE = "删除";
	private final String STR_POP_REMAIL = "回信";
	private final String STR_POP_BACK = "返回";

	private void keyLogicOnStateRecv()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			close();
			return;
		}
		switch (this.m_groupId) {
			case GROUP_MAIL_ITEMS:
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
					if (getGroupIndex() == 0) {
						this.m_index = INDEX_MAIL_ITEMS_LAST;
					}
					else {
						this.setWindowIndex(this.m_index - 1);
					}
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
					if (getGroupIndex() == NUM_COLS_ITEMS) {
						this.m_index = INDEX_MAIL_ITEMS_FIRST;
					}
					else {
						this.setWindowIndex(this.m_index + 1);
					}
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					final String[] temp = getRecvItemStrs();
					final UIPopmenu popmenu = (UIPopmenu) UIManager.getInstance().createWindow(
							UIWindowFactory.WINDOW_POPMENU);
					popmenu.setPopmenus(temp);
					popmenu.addListener(new UIListener() {

						public void actionPerformed()
						{
							if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
										|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
								int index = popmenu.getCurrentIndex();
								String tempStr = temp[index];
								if (tempStr == STR_POP_RECVALL) {
									Tools.debugPrintln("获取全部附件");
									int num_mail = 0;
									if (m_mail_items != null) {
										for (int i = 0; i < m_mail_items.length; i++) {
											if (m_mail_items[i] != null) {
												num_mail++;
											}
										}
									}
									GameManager.getInstance().m_game_message.sendMailRecvAcc(m_mail_id, num_mail);
								}
								else if (tempStr == STR_POP_MOVETOCOL) {
									GameManager.getInstance().m_game_message.sendMailCollectMail(new long[] { m_mail_id });
									sendMailListDeleteRequset();
									close();
								}
								else if (tempStr == STR_POP_DELETE) {
									final UIMessageBox messageBox = (UIMessageBox) UIManager.getInstance()
											.createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
									messageBox.setMessage("是否删除邮件?");
									messageBox.addListener(new UIListener() {

										public void actionPerformed()
										{
											if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
													|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
												GameManager.getInstance().m_game_message.sendMailDelete(new long[] { m_mail_id });
												sendMailListDeleteRequset();
												close();
												UIManager.getInstance().removeWindow(messageBox);
											}
											else if (AbstractGameScreen
													.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
												UIManager.getInstance().removeWindow(messageBox);
											}
										}
									});
								}
								else if (tempStr == STR_POP_REMAIL) {
									close();
									sendMailToPlayer(m_mail_name_sender);
								}
								UIManager.getInstance().removeWindow(popmenu);
							}
							else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
								UIManager.getInstance().removeWindow(popmenu);
							}
						}
					});
				}
				else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					close();
				}
				break;
			default:
				break;
		}
	}

	private void sendMailListDeleteRequset()
	{
		UIMailList list = (UIMailList) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_MAIL_LIST);
		if (list == null) {
			return ;
		}
		list.sendMailListRequest();
	}

	private String[] getRecvItemStrs()
	{
		boolean isGotData = false;
		if (m_mail_items != null) {
			Tools.debugPrintln("<<<<<<<<>>>>>>>>>m_mail_items.length="+m_mail_items.length);
			for (int i = 0; i < m_mail_items.length; i++) {
				Tools.debugPrintln("m_mail_money_game_send="+m_mail_money_game_send);
				if (m_mail_items[i] != null || m_mail_money_game_send != 0) {
					isGotData = true;
					break;
				}
			}
		}
		String[] temp = null;
		Tools.debugPrintln("m_mailType="+m_mailType);
		switch (m_mailType) {
			case MailItem.TYPE_MAIL_NOR:
				if (isGotData) {
					temp = new String[] { STR_POP_RECVALL, STR_POP_MOVETOCOL, STR_POP_DELETE, STR_POP_REMAIL,
							STR_POP_BACK };
				}
				else {
					temp = new String[] { STR_POP_MOVETOCOL, STR_POP_DELETE, STR_POP_REMAIL, STR_POP_BACK };
				}
				break;
			case MailItem.TYPE_MAIL_COL:
				if (isGotData) {
					temp = new String[] { STR_POP_RECVALL, STR_POP_DELETE, STR_POP_REMAIL, STR_POP_BACK };
				}
				else {
					temp = new String[] { STR_POP_DELETE, STR_POP_REMAIL, STR_POP_BACK };
				}
				break;
			case MailItem.TYPE_MAIL_SYS:
				if (isGotData) {
					temp = new String[] { STR_POP_RECVALL, STR_POP_DELETE, STR_POP_BACK };
				}
				else {
					temp = new String[] { STR_POP_MOVETOCOL, STR_POP_BACK };
				}
				break;
		}
		if (m_mail_name_sender.equals("邮件系统")) {
			temp = Tools.getObjectArrayExceptSpecObject(temp, STR_POP_REMAIL);
		}
		return temp;
	}

	private void gotoWriteContentFrom()
	{
		switch (m_state) {
			case STATE_BASE_WRITE_FAMILY:
			case STATE_BASE_WRITE_GM:
				gotoFrom2();
				break;
			default:
				gotoFrom1();
				break;
		}
	}

	// 点击标题或者收信人或者内容框,进入高级界面
	private void gotoFrom1()
	{
		Form myForm = new Form("请输入邮件信息");
		if (this.m_state == STATE_BASE_WRITE_FAMILY) {
			gotoWriteFamilyContentFrom();
			return;
		}
		final TextField textField1 = new TextField("请输入标题", m_mail_title, ActorRole.NUM_MAX_MAIL_TITLE, TextField.ANY);
		final TextField textField2 = new TextField("请输入收件人名称", m_mail_name_recver, ActorRole.NUM_MAX_NAME,
				TextField.ANY);;
		final TextField textField3 = new TextField("请输入内容", m_mail_content, ActorRole.NUM_MAX_MAIL_CONTENT,
				TextField.ANY);
		myForm.append(textField1);
		myForm.append(textField2);
		myForm.append(textField3);
		final StringItem warnInfoItem = new StringItem("", "");
		myForm.append(warnInfoItem);
		final Command ok = new Command("确定", Command.OK, 1);
		final Command cancel = new Command("返回", Command.CANCEL, 1);
		myForm.addCommand(ok);
		myForm.addCommand(cancel);
		CommandListener listener = new CommandListener() {

			public void commandAction(Command c, Displayable d)
			{
				if (c == ok) {
					if (textField1.getString().equals("")) {
						warnInfoItem.setText("请输入标题");
					}
					else if (textField2.getString().equals("")) {
						warnInfoItem.setText("请输入收件人名称");
					}
					// else if (textField3.getString().equals("")) {
					// warnInfoItem.setText("请输入内容");
					// }
					else {
						m_mail_title = textField1.getString();
						m_mail_name_recver = textField2.getString();
						changeContent(textField3.getString());
						Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
					}
				}
				else if (c == cancel) {
					Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
				}
			}
		};
		myForm.setCommandListener(listener);
		Display.getDisplay(GameMIDlet.s_instance).setCurrent(myForm);
	}

	// 点击标题或者收信人或者内容框,进入高级界面
	private void gotoFrom2()
	{
		Form myForm = new Form("请输入邮件信息");
		if (this.m_state == STATE_BASE_WRITE_FAMILY) {
			gotoWriteFamilyContentFrom();
			return;
		}
		final TextField textField1 = new TextField("请输入标题", m_mail_title, ActorRole.NUM_MAX_MAIL_TITLE, TextField.ANY);
		final TextField textField3 = new TextField("请输入内容", m_mail_content, ActorRole.NUM_MAX_MAIL_CONTENT,
				TextField.ANY);
		myForm.append(textField1);
		myForm.append(textField3);
		final StringItem warnInfoItem = new StringItem("", "");
		myForm.append(warnInfoItem);
		final Command ok = new Command("确定", Command.OK, 1);
		final Command cancel = new Command("返回", Command.CANCEL, 1);
		myForm.addCommand(ok);
		myForm.addCommand(cancel);
		CommandListener listener = new CommandListener() {

			public void commandAction(Command c, Displayable d)
			{
				if (c == ok) {
					if (textField1.getString().equals("")) {
						warnInfoItem.setText("请输入标题");
					}
					else {
						m_mail_title = textField1.getString();
						changeContent(textField3.getString());
						Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
					}
				}
				else if (c == cancel) {
					Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
				}
			}
		};
		myForm.setCommandListener(listener);
		Display.getDisplay(GameMIDlet.s_instance).setCurrent(myForm);
	}

	// 点击标题或者收信人或者内容框,进入高级界面
	private void gotoWriteFamilyContentFrom()
	{
		final Form myForm = new Form("请输入邮件信息");
		final TextField textField1 = new TextField("请输入标题", m_mail_title, 6, TextField.ANY);
		final TextField textField3 = new TextField("请输入内容", m_mail_content, 50, TextField.ANY);
		myForm.append(textField1);
		myForm.append(textField3);
		final StringItem warnInfoItem = new StringItem("", "");
		myForm.append(warnInfoItem);
		final Command ok = new Command("确定", Command.OK, 1);
		final Command cancel = new Command("返回", Command.CANCEL, 1);
		myForm.addCommand(ok);
		myForm.addCommand(cancel);
		CommandListener listener = new CommandListener() {

			public void commandAction(Command c, Displayable d)
			{
				if (c == ok) {
					if (textField1.getString().equals("")) {
						warnInfoItem.setText("请输入标题");
					}
					// else if (textField3.getString().equals("")) {
					// warnInfoItem.setText("请输入内容");
					// }
					else {
						m_mail_title = textField1.getString();
						changeContent(textField3.getString());
						Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
					}
				}
				else if (c == cancel) {
					Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
				}
			}
		};
		myForm.setCommandListener(listener);
		Display.getDisplay(GameMIDlet.s_instance).setCurrent(myForm);
	}

	private void gotoWriteMoneyForm()
	{
		Form myForm = new Form("输入交易金额");
		final short[] money = ActorRole.getMoney(m_mail_money_game_send);
		final TextField textField1 = new TextField("金：", money[0] == 0 ? "" : String.valueOf(money[0]), 4,
				TextField.NUMERIC);
		final TextField textField2 = new TextField("银：", money[1] == 0 ? "" : String.valueOf(money[1]), 2,
				TextField.NUMERIC);
		final TextField textField3 = new TextField("铜：", money[2] == 0 ? "" : String.valueOf(money[2]), 2,
				TextField.NUMERIC);

		myForm.append(textField1);
		myForm.append(textField2);
		myForm.append(textField3);
		short[] moneyGame = ActorRole.getMoney(GameManager.getInstance().m_role.m_money_game);
		myForm.append("当前拥有金 : " + moneyGame[0] + " ,银 ：" + moneyGame[1] + " ,铜 ：" + moneyGame[2]);
		final StringItem info = new StringItem("", "");
		myForm.append(info);

		final Command ok = new Command("确定", Command.OK, 1);
		final Command cancel = new Command("返回", Command.CANCEL, 1);
		myForm.addCommand(ok);
		myForm.addCommand(cancel);
		//
		CommandListener listener = new CommandListener() {

			public void commandAction(Command c, Displayable d)
		{
			if (c == ok) {
				int gold = UITools.checkMoney(textField1.getString());
				switch (gold) {
					case -1:
					case -2:
						info.setText("请输入正确数据金钱");
						textField1.setString(String.valueOf(money[0]));
						return;
				}
				//
				int silver = UITools.checkMoney(textField2.getString());
				switch (silver) {
					case -1:
					case -2:
						info.setText("请输入正确数据金钱");
						textField2.setString(String.valueOf(money[1]));
						return;
				}
				//
				int copper = UITools.checkMoney(textField3.getString());
				switch (copper) {
					case -1:
					case -2:
						info.setText("请输入正确数据金钱");
						textField3.setString(String.valueOf(money[2]));
						return;
				}
				short[] temp = new short[3];
				temp[0] = (short) gold;
				temp[1] = (short) silver;
				temp[2] = (short) copper;

				long tempMoney = ActorRole.getMoney(temp);
				if (tempMoney > GameManager.getInstance().m_role.m_money_game) {
					info.setText("当前金额不足");
				}
				else {
					Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
					m_mail_money_game_send = tempMoney;
					// m_mail_money_game_now = GameManager.getInstance().m_role.m_money_game - tempMoney;
				}
			}
			else if (c == cancel) {
				Display.getDisplay(GameMIDlet.s_instance).setCurrent(GameMIDlet.s_canvas);
			}
		}
		};
		myForm.setCommandListener(listener);

		Display.getDisplay(GameMIDlet.s_instance).setCurrent(myForm);
	}

	/***************************/
	// 数据接收
	/***************************/

	public void recvMessage(int orderId, DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("recvMessage mail orderID = " + orderId);
		switch (orderId) {
			case GameProtocol.CS_MAIL_SENDMAIL:
				recvMailSend(dis);
				break;
			case GameProtocol.CS_MAIL_ACC:
				recvMailAcc(dis);
				break;
		}
	}

	private void recvMailAcc(DataInputStream dis) throws IOException
	{
		byte state = dis.readByte();
		if (state == 0) {
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "背包已满");
		}
		else {
			m_mail_items = new ActorItem[NUM_COLS_ITEMS];
			m_mail_money_game_send = 0;
			// m_mail_money_game_now = GameManager.getInstance().m_role.m_money_game;
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "获取附件成功");
		}
	}

	/**
	 * 发送邮件返回
	 * @param dis
	 * @throws IOException
	 */
	private void recvMailSend(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("recvMailSend ");
		short state = dis.readShort();
		if (state == 1) {
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "发送成功");
			close();
			return;
		}
		String info = dis.readUTF();
		GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, info);
	}

	public static void recvMailDetail(DataInputStream dis) throws IOException
	{
		byte state = dis.readByte();
		if (state == 0) {
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "获取邮件失败");
			return;
		}
		UIMail mail = (UIMail) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_MAIL);
		mail.m_mailType = dis.readByte();
		mail.m_mail_id = dis.readLong();
		mail.m_mail_title = dis.readUTF();
//		 mail.m_mail_time = MailItem.getTime(dis.readLong());
		mail.m_mail_name_sender = dis.readUTF();
		mail.m_mail_name_recver = dis.readUTF();
		mail.changeContent(dis.readUTF());
		mail.m_mail_money_game_send = dis.readLong();
		int num_items = dis.readInt();
		for (int i = 0; i < num_items; i++) {
			byte type = dis.readByte();
			ActorItem item = null;
			switch (type) {
				case ActorItem.TYPE_GOODS_EQUIPMENT:
					item = new ActorItemEquip();
					break;
				default:
					item = new ActorItem();
					break;
			}
			item.m_type = type;
			item.m_guid = dis.readLong();
			item.m_name = dis.readUTF();
			item.m_num = dis.readInt();// 数目
			item.m_iconId = dis.readInt();
			item.m_iconCoverId = dis.readInt();
			if (type == ActorItem.TYPE_GOODS_EQUIPMENT) {// 装备
				ActorItemEquip equip = (ActorItemEquip) item;
				byte holeNum = dis.readByte();
				if (holeNum > 0) {
					equip.m_color_jewels = new byte[holeNum];
					equip.m_isXiangqian_jewels = new boolean[holeNum];
					for (int j = 0; j < holeNum; j++) {
						equip.m_color_jewels[j] = dis.readByte();
						equip.m_isXiangqian_jewels[j] = dis.readBoolean();
					}
				}
			}
			mail.m_mail_items[i] = item;
		}
		// 将其标记为已读
		MailItem item = UIMailList.getMailItemById(mail.m_id);
		if (item != null) {
			if (item.m_state == MailItem.STATE_MAIL_NEW || item.m_state == MailItem.STATE_MAIL_UNREAD) {
				MailItem.s_num_newMail--;
			}
			item.m_state = MailItem.STATE_MAIL_READ;
		}
		mail.setWindowStatus(STATE_BASE_RECV);
		mail.setWindowIndex(INDEX_MAIL_ITEMS_FIRST);
		UIManager.getInstance().addWindow(mail);
	}

	/***************************/
	// 绘制
	/***************************/
	protected void paintContent(Graphics g)
	{
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, m_title);
		int tempX = UITools.INITX_CONTENT + m_x;
		int tempY = m_y + UITools.INITY_CONTENT;
		switch (this.m_state) {
			case STATE_BASE_ADD: {
				m_bag_handler.setInitXY(tempY);
				m_bag_handler.paintLabel(g);
				m_bag_handler.paintBag(g);
				m_bag_handler.paintDrag(g);
				m_bag_handler.paintBagFocused(g);
				m_bag_handler.paintDescribe(g);
				//
				tempY += 150;
				paintItemAndMoney(g, tempX, tempY);
			}
				break;
			case STATE_BASE_WRITE:
			case STATE_BASE_WRITE_FAMILY:
			case STATE_BASE_WRITE_GM:
			case STATE_BASE_RECV: {
				paintTitleAndRecver(g, tempX, tempY);
				tempY += (H_TITLEANDRECVER + UITools.IH_RECT_CONTENT);
				paintContents(g, tempX, tempY);
				tempY += (m_h_contents + UITools.IH_RECT_CONTENT);
				paintItemAndMoney(g, tempX, tempY);
			}
				break;
		}
	}

	private void paintTitleAndRecver(Graphics g, int x, int y)
	{
		UITools.paintContentRect(g, x, y, UITools.W_RECT, H_TITLEANDRECVER);
		//
		int tempX = x + UITools.IW_RECT_CONTENT;
		int tempY = y + UITools.IW_RECT_CONTENT;
		// 属性
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		g.drawString(TITLE_ATT, tempX, tempY, 0);
		tempY += H_PRE_TITLE;
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		if (m_state == STATE_BASE_RECV) {//收信
			g.drawString(SENDER_ATT, tempX, tempY, 0);
		}
		else {
			g.drawString(RECVER_ATT, tempX, tempY, 0);
		}
		// title And recver
		tempX += AbstractGameScreen.s_font.stringWidth(SENDER_ATT) + 5;
		tempY = y + UITools.IW_RECT_CONTENT;
		UITools.paintInputRectLT(g, tempX, tempY, W_TITLE, H_PRE_TITLE, m_mail_title,
				this.m_groupId == GROUP_MAIL_TITLE);
		tempY += H_PRE_TITLE;
		if (m_state == STATE_BASE_RECV) {
			UITools.paintInputRectLT(g, tempX, tempY, W_RECVER, H_PRE_TITLE, m_mail_name_sender,
					this.m_groupId == GROUP_MAIL_RECVER);
		}
		else {
			UITools.paintInputRectLT(g, tempX, tempY, W_RECVER, H_PRE_TITLE, m_mail_name_recver,
					this.m_groupId == GROUP_MAIL_RECVER);
		}
		tempX += W_RECVER;
		// add
		boolean isFocused = this.m_groupId == GROUP_MAIL_LIST_SOCIAL;
		tempX += UITools.IW_RECT_CONTENT;
		UITools.paintInputRectLT(g, tempX, tempY, W_ADD, H_PRE_TITLE, TITLE_ADD_SOCIAL, isFocused);
		Image image = null;
		if (isFocused) {
			image = UIManager.IMAGE_ARR_LIGHT;
		}
		else {
			image = UIManager.IMAGE_ARR_DARK;
		}
		tempX += W_ADD - 2;
		tempY += 5;
//		Tools.drawRegion(g, image, tempX, tempY, Tools.TRANS_HV, Tools.RIGHT_TOP);
	}

	private void paintItemAndMoney(Graphics g, int x, int y)
	{
		UITools.paintContentRect(g, x, y, UITools.W_RECT, H_MONEY);
		int tempX = x + UITools.IW_RECT_CONTENT;
		int tempY = y + UITools.IW_RECT_CONTENT;
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		g.drawString(ACCESSARY_ATT, tempX, tempY, 0);
		// 绘制Item
		int groupId = this.m_state == STATE_BASE_ADD ? GROUP_ADD_ITEMS : GROUP_MAIL_ITEMS;
		paintActorItems(g, tempX + 65, tempY, groupId);
		//
		tempY += UITools.H_PRE;
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		if (m_state == STATE_BASE_RECV) {
			g.drawString(MONEY_SEND_ATT_RECV, tempX, tempY, 0);
		}else{
			g.drawString(MONEY_SEND_ATT_ADD, tempX, tempY, 0);
		}
		// 金钱
		tempY += ((UITools.H_PRE - UITools.H_MONEY_GAME));
		UITools.paintMoneyGameRB(g, x + UITools.W_RECT - UITools.IW_RECT_CONTENT - 1, y + H_MONEY
				- UITools.IH_RECT_CONTENT - 1, m_mail_money_game_send,
				this.m_groupId == GROUP_MAIL_MONEY_SEND);
	}

	private void paintActorItems(Graphics g, int x, int y, int group)
	{
		int tempX = x;
		boolean isFocusAble = this.m_groupId == group;
		int groupIndex = getGroupIndex();
		ActorItem[] items = this.m_state == STATE_BASE_ADD ? m_mail_items_temp : m_mail_items;
		for (int i = 0; i < NUM_COLS_ITEMS; i++) {
			ActorItem item = null;
			if (items != null) {
				item = items[i];
			}
			UITools.paintActorItem(g, tempX, y, item, true, isFocusAble && groupIndex == i, true);
			tempX += UIManager.IMAGE_ITEM_OPEN.getWidth() + 2;
		}
		if (items != null && items[0] != null) {
			g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
			g.drawString(items[0].m_name, tempX, y, 0);
		}
	}

	private void paintContents(Graphics g, int x, int y)
	{
		UITools.paintContentRect(g, x, y, UITools.W_RECT, m_h_contents);
		if (m_mail_contents == null) {
			return;
		}
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();
		int tempX = x + UITools.IW_RECT_CONTENT;
		int tempY = y + UITools.IH_RECT_CONTENT;
		g.setClip(tempX, tempY, UITools.W_RECT - (UITools.IW_RECT_CONTENT << 1), m_h_contents
				- (UITools.IH_RECT_CONTENT << 1));
		g.setColor(UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS));
		for (int j2 = 0; j2 < m_mail_contents.length; j2++) {
			if (m_mail_contents[j2] == null) {
				continue;
			}
			g.drawString(m_mail_contents[j2], tempX, tempY, 0);
			tempY += AbstractGameScreen.FONT_H;
		}
		g.setClip(clipX, clipY, clipW, clipH);
	}

	/***************************/
	// 接收Message
	/***************************/

	public static final void recvMailContent(DataInputStream dis) throws IOException
	{
		Tools.debugPrintln("UiMail recvMailContent");
		UIMail mail = (UIMail) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_MAIL);
		mail.m_mail_name_sender = dis.readUTF();
		mail.m_mail_name_recver = dis.readUTF();
		mail.m_mail_content = dis.readUTF();
		mail.m_mail_money_game_send = dis.readLong();
		int num = dis.readInt();
		if (num == 0) {
			mail.m_mail_items = null;
		}
		else {
			mail.m_mail_items = new ActorItem[num];
			for (int i = 0; i < num; i++) {
				mail.m_mail_items[i] = new ActorItem();
				mail.m_mail_items[i].m_key = dis.readUTF();
				mail.m_mail_items[i].m_num = dis.readInt();
				mail.m_mail_items[i].m_name = dis.readUTF();
				mail.m_mail_items[i].m_iconId = dis.readShort();
				mail.m_mail_items[i].m_iconCoverId = dis.readShort();
			}
		}
		UIManager.getInstance().addWindow(mail);
	}

	/**
	 * 2012-2-17 上午10:36:36 venizeng
	 * @param dis
	 * @throws IOException
	 */
	public static void recvMailFamily(DataInputStream dis) throws IOException
	{
		byte state = dis.readByte();
		switch (state) {
			case 0:
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "家族邮件发送失败");
				break;
			case 1:
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "家族邮件发送成功");
				break;
			case 2:
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "权限不够，不能发送家族邮件");
				break;
			case 3:
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "无此家族");
				break;
			default:
				break;
		}

		UIMail mail = (UIMail) UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_MAIL);
		if (mail != null) {
			UIManager.getInstance().removeWindow(mail);
		}
	}
	private int[][] lableRect,bagRect;
	protected void registerTouch() {
		clearAllRects();
		registerNormalSoft();
		switch (this.m_state) {
			case STATE_BASE_ADD: {
				
				int tempY1 = m_y + UITools.INITY_CONTENT;
				m_bag_handler.setInitXY(tempY1);
				int tempX = m_bag_handler.m_x_label;
				int tempY = m_bag_handler.m_y_label;
				int lableSize = m_bag_handler.m_num_label;
				lableRect = new int[lableSize][];
				for (int i = 0; i < lableSize; i++) {
				lableRect[i] = new int[] { tempX, tempY,
						m_bag_handler.m_w_label, UITools.H_LABEL };
						tempX += (m_bag_handler.m_w_label + m_bag_handler.m_iw_label);
				}
				initTouchRect(lableRect);
				/////////////////////////
//				int openDisIndex = ActorRole.s_num_open - m_bag_handler.m_index_data_top;
				tempY = m_bag_handler.m_y_bag;
//				ActorItem item = null;
//				int num = 0;
				int bagSize = m_bag_handler.m_row*m_bag_handler.NUM_BAG_COL;
				bagRect = new int[bagSize][];
//				boolean isAll = getCurrentLabelId() == ActorItem.TYPE_GOODS_ALL;
				for (int i = 0; i < m_bag_handler.m_row; i++) {
					tempX = m_bag_handler.m_x_bag;
					for (int j = 0; j < m_bag_handler.NUM_BAG_COL; j++) {
					bagRect[i * m_bag_handler.NUM_BAG_COL + j] = new int[] {
							tempX, tempY, m_bag_handler.W_BAG,
							m_bag_handler.H_BAG };
						tempX += m_bag_handler.RW_BAG;
					}
					tempY += m_bag_handler.RH_BAG;
				}
				initTouchRect(bagRect);
				int[][]rectdir = new int[][]{
						{332,63,19,23},
						{332,126,19,23},
				};
				initTouchRect(rectdir);
			}
			break;
			case STATE_BASE_WRITE:
			case STATE_BASE_WRITE_FAMILY:
			case STATE_BASE_WRITE_GM:
			case STATE_BASE_RECV: {
				int[][]rect = new int[][]{
						{65,43,390,18},	
						{65,65,355,19},	
						{426,65,30,20},	//add
						{22,94,437,126},//content
						{89,228,19,19},//item
						{365,245,92,19},//money
						
				};
				initTouchRect(rect);
			}
			break;
		}
		
	}

	protected void onTouchRect(int[][] rect, int touchid) {
		switch (this.m_state) {
		case STATE_BASE_ADD: {
			if (rect == lableRect) {
				if (m_index != touchid+INDEX_ADD_LABEL_FIRST) {
					setWindowIndex(touchid+INDEX_ADD_LABEL_FIRST);
				} else {
					GameTouchManager.keyPressedFire();
				}
			} else if (rect == bagRect) {
				if (m_index != touchid+INDEX_ADD_BAG_FIRST) {
					setWindowIndex(touchid+INDEX_ADD_BAG_FIRST);
				} else {
					GameTouchManager.keyPressedFire();
				}
			} else {
				switch(touchid) {
				case 0:
					GameTouchManager.keyPressedDir(1);
					break;
				case 1:
					GameTouchManager.keyPressedDir(3);
					break;
				}
			}
		}
			break;
		case STATE_BASE_WRITE:
		case STATE_BASE_WRITE_FAMILY:
		case STATE_BASE_WRITE_GM:
		case STATE_BASE_RECV: {
			switch (touchid) {
			case 0:
				this.setWindowIndex(INDEX_MAIL_TITLE);
				break;
			case 1:
				this.setWindowIndex(INDEX_MAIL_RECVER);
				break;
			case 2:
				this.setWindowIndex(INDEX_MAIL_LIST_SOCIAL);
				break;
			case 3:
				this.setWindowIndex(INDEX_MAIL_CONTENT);
				break;
			case 4:
				this.setWindowIndex(INDEX_MAIL_ITEMS_FIRST);
				break;
			case 5:
				this.setWindowIndex(INDEX_MAIL_MONEY_SEND);
				break;
			}
			GameTouchManager.keyPressedFire();
		}
			break;
		}
		
	}

}
